package com.zcl.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcl.dao.Loss;
import com.zcl.service.LossService;

@Controller
public class LossController {
	@Autowired
	LossService lossService;
	/**
	 * 处理	/loss
	 * @return	船舶信息页面
	 */
	@RequestMapping("/loss")
	public String lossIndex() {
		return "loss";
	}
	/**
	 * 事故信息概况查询
	 */
	@ResponseBody
	@RequestMapping("/loss/surveryQuery")
	public List<Map<String, String>> accidentQuery(Model model) {
		List<Map<String, String>> lossList = lossService.lossQuery();
		return lossList;
	}
	
	/**
	 * 事故信息概况-操作-详情
	 * @return
	 */
	@RequestMapping("/lossSurveryInfo")
	public String accidentSuveryInfo(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Loss loss = lossService.lossSuveryInfo(map);
		model.addAttribute("loss", loss);
		System.out.println("事故信息概况-操作-详情:accidentId="+accidentId);
		return "loss_information";
	}
	
	/**
	 * 事故信息概况-操作-编辑
	 * @return
	 */
	@RequestMapping("/lossSurveryRevise")
	public String lossSuveryRevise(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId",accidentId);
		Loss loss = lossService.lossSuveryInfo(map);
		model.addAttribute("loss", loss);
		System.out.println("事故信息概况-操作-编辑:accidentId="+accidentId);
		return "loss_revise";
	}
	
	/**
	 * 事故信息概况-操作-编辑2--获取某些下拉框值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/lossSurveryRevise2")
	public Loss lossSurveryRevise2(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Loss loss = lossService.lossSuveryInfo(map);
		
//		model.addAttribute("accident", accident);
		System.out.println("事故信息概况-操作-编辑2--获取某些下拉框值:accidentId="+accidentId);
		return loss;
	}
	
	/**
	 * 事故信息概况-操作-删除
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loss/delete")
	public void accidentDelete(@RequestParam("accidentId") String accidentId) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		lossService.lossDelete(map);
		System.out.println("删除");;
	}
	
	/**
	 * 事故添加页面
	 * @return
	 */
	@RequestMapping("/lossAdd")
	public String accidentAccidentAdd() {
		return "loss_add";
	}
	
	/**
	 * 事故信息查询-全文搜索
	 * @param lossrch_input
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loss/globalQuery")
	public List<Map<String, String>> accidentGlobalQuery(@RequestParam("search_input") String lossrch_input){
		Map<String, String> map = new HashMap<String,String>();
		map.put("search_input", lossrch_input);
		List<Map<String, String>> accidentList = lossService.lossGlobalQuery(map);
		return accidentList;
		
	} 
	
	/**
	 * 事故信息查询-高级搜索
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loss/advancedQuery")
	public List<Map<String, String>> accidentAdvancedQuery(HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		
		map.put("accidentId", request.getParameter("accidentId"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("lossLevel", request.getParameter("lossLevel"));
		map.put("number", request.getParameter("number"));
		map.put("money", request.getParameter("money"));
		map.put("environment", request.getParameter("environment"));
		List<Map<String, String>> lossList = lossService.lossAdvancedQuery(map);
		return lossList;
		
	} 
	
	/**
	 * 事故信息录入
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/loss/addLoss")
	public void accidentAdd(HttpServletRequest request) throws UnsupportedEncodingException {
		String accidentId = UUID.randomUUID().toString();
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("lossLevel", request.getParameter("lossLevel"));
		map.put("number", request.getParameter("number"));
		map.put("money", request.getParameter("money"));
		map.put("environment", request.getParameter("environment"));
		lossService.lossAdd(map);
	}
	
	/**
	 * 事故信息修改
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/loss/reviseLoss")
	public void accidentRevise(HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", request.getParameter("accidentId"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("lossLevel", request.getParameter("lossLevel"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("number", request.getParameter("number"));
		map.put("money", request.getParameter("money"));
		map.put("environment", request.getParameter("environment"));
		lossService.lossRevise(map);
	}
}
